/**
 * 旅游wifi 管理员界面, 暂时提供批量查询的功能
 * Created by dongchen on 16/4/25.
 */
define([
    "jquery",
    "lodash",
    "ViewBase",
    "js/module/admin/adminModel",
    "js/module/single/singleView",
    "js/module/charge/chargeView",
    "Router",
    "bootbox",
    "bootstrap",
    "metisMenu"
], function(
    $,
    _,
    ViewBase,
    model,
    singleView,
    chargeView,
    Router,
    bootbox
) {
    var tplPath = '/js/module/admin/admin.tpl';

    var view = ViewBase.extend({
        getTplById : function (tplId, data){
            return _.template(this.getTpl(tplPath, tplId))(data);
        },

        events : {
            "click -> .upload-add-mobiles" : "onClickUploadMobilesHandler",
            "click -> .upload-init-mobiles" : "onClickInitMobilesHandler",
            "click -> .btn-discount-save" : "onClickSaveDiscountHandler"
        },

        init : function(opts){
            view.__super__.init(opts);
            this.model = new model();

            this.loadCss('/metisMenu/dist/metisMenu.css');
            this.loadCss('/js/module/admin/admin.css');
        },

        render : function(){
            this.target.html(this.getTplById('admin-ctn-tpl'));
            this.$nav = this.target.find('.admin-nav-left-ctn');
            this.$content = this.target.find('.admin-content-ctn');

            this.renderLeftNav();
        },

        /**
         * 渲染左边栏,并默认选中第一项
         */
        renderLeftNav : function(){
            var
                $menu,
                $nav = this.$nav;

            $nav.html(this.getTplById('admin-ctn-nav-left'));

            $menu = this.target.find('#side-menu');
            $menu.metisMenu();

            var _this = this;
            var router = new Router({
                'query-batch' : function(){
                    _this.renderQueryBatchView();
                },
                'query-single' : function(){
                    _this.renderQuerySingleView();
                },
                'manager' : function(){
                    _this.renderAdminView();
                },
                'order-manager' : function(){
                    _this.renderChargeUserView();
                },
                'package-manager' : function(){
                    _this.renderSettingsView();
                }
            });
            router.init();
            if(window.location.hash === ''){
                $menu.find('li:first a span').trigger('click');
            }
        },

        renderQueryBatchView : function(){
            this.$content.html(this.getTplById('admin-ctn-batch-tpl'));
        },
        renderQuerySingleView : function(){
            new singleView({pid : 'page-single', el : this.$content}).render();
        },
        renderAdminView : function(){
            this.$content.html(this.getTplById('admin-ctn-manager-tpl'));
        },
        renderChargeUserView : function(){
            new chargeView({pid : 'charge-user', el : this.$content}).render();
        },
        renderSettingsView : function(){
            var _this = this;
            this.$content.html(this.getTplById('admin-package-manager-tpl'));

            this.model.getDiscount().done(function(res){
                $('.discount-ctn').html(_this.getTplById('admin-setting-discount-tpl', {discount : res}));
            });
        },

        onClickSaveDiscountHandler : function(){
            var $discounts = $('.discount-value');
            var postData = {};
            _.each($discounts, function (el) {
                var key = $(el).data('sp');
                var value = $(el).val();
                postData[key] = value;
            });

            this.model.saveDiscount(postData).done(function(res){
                bootbox.alert('修改成功');
            });
        },

        onClickUploadMobilesHandler : function(){
            var $fileEl = $('#addMobiles');
            var val = $fileEl.val();
            if(val){
                this.model.uploadAddMobiles($fileEl).done(function(res){
                    if(res.code === 200){
                        bootbox.alert('上传成功');
                    }else{
                        bootbox.alert(res.msg);
                    }
                });
            }else{
                bootbox.alert('请先选择要上传的文件');
            }
        },

        onClickInitMobilesHandler : function(){
            var $fileEl = $('#initMoblies');
            var val = $fileEl.val();
            var openDate = $('.mobiles-init-date').val();

            if(!openDate){
                bootbox.alert('请选择开卡日期');
                return;
            }

            if(!val){
                bootbox.alert('请先选择要上传的文件');
                return;
            }

            this.model.uploadInitMobiles($fileEl, {openDate : openDate}).done(function (res) {
                if (res.code === 200) {
                    bootbox.alert('上传成功');
                } else {
                    bootbox.alert(res.msg);
                }
            });
        }
    });

    return view;
});
